package com.tools.developer.fragments;

import java.util.ArrayList;

import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.tools.developer.R;
import com.tools.developer.app.AppDetailActivity;
import com.tools.developer.app.AppDetailActivity4Running;
import com.tools.developer.app.AppModel;
import com.tools.developer.app.AppUtil;

public class AppListFragment extends ListFragment implements
		OnItemClickListener, OnClickListener {

	private static final int MSG_LOADING_LIST = 0x1001;
	private static final int MSG_REFRESH_LIST = 0x1002;

	private static View mWaitLayout;
	private static ListView mListV;
	private int mCatalogType = AppModel.APP_TYPE_ALL;
	private static ArrayList<AppModel> mAppList = new ArrayList<AppModel>();
	private MyHandler mHandler;
	private AppListThread mListThread;
	private Context mCtx;

	public AppListFragment(Context ctx) {
		super();
		this.mCtx = ctx;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View contentView = inflater.inflate(R.layout.fragment_applist, null);
		mWaitLayout = contentView
				.findViewById(R.id.software_load_waiting_layout);
		mWaitLayout.setVisibility(View.VISIBLE);

		mListV = (ListView) contentView.findViewById(android.R.id.list);
		mListV.setVisibility(View.INVISIBLE);
		mListV.setOnItemClickListener(this);
		mHandler = new MyHandler(mCtx);
		mListThread = new AppListThread(mHandler, AppModel.APP_TYPE_ALL);
		mListThread.start();
		return contentView;
	}

	class AppListThread extends Thread {
		MyHandler mHandler;
		int mType;

		AppListThread(MyHandler handler, int type) {
			mHandler = handler;
			mType = type;
		}

		@Override
		public void run() {
			switch (mType) {
			case AppModel.APP_TYPE_ALL:
				mAppList = AppUtil.getInstalledAppList(mCtx);
				break;
			case AppModel.APP_TYPE_RUNNING:
				mAppList = AppUtil.getRunningAllAppList(mCtx);
				break;
			case AppModel.APP_TYPE_SYS:
				mAppList = AppUtil.getInstalledRomAppList(mCtx);
				break;
			default:
				mAppList = AppUtil.getInstalledAppList(mCtx);
				break;
			}
			mHandler.sendEmptyMessage(MSG_REFRESH_LIST);

		}
	}

	private static class AppListAdapter extends BaseAdapter {
		private ArrayList<AppModel> mAppList;
		private Context mCtx;

		public AppListAdapter(Context ctx, ArrayList<AppModel> appList) {
			mCtx = ctx;
			mAppList = appList;
		}

		@Override
		public int getCount() {
			return mAppList.size();
		}

		@Override
		public AppModel getItem(int position) {
			return mAppList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (mCtx != null) {
				ViewHolder holder = null;
				if (convertView == null) {
					LayoutInflater inflater = (LayoutInflater) mCtx
							.getSystemService(IntentService.LAYOUT_INFLATER_SERVICE);
					convertView = inflater.inflate(R.layout.app_list_item,
							null, false);
					holder = new ViewHolder();
					holder.iconView = (ImageView) convertView
							.findViewById(R.id.icon);
					holder.titleView = (TextView) convertView
							.findViewById(R.id.title);
					holder.pkgName = (TextView) convertView
							.findViewById(R.id.pkg_name);
					holder.ver = (TextView) convertView
							.findViewById(R.id.app_ver);
					holder.shareUid = (TextView) convertView
							.findViewById(R.id.app_share_uid);
					convertView.setTag(holder);

				} else {
					holder = (ViewHolder) convertView.getTag();
				}

				AppModel app = getItem(position);
				PackageManager pm = mCtx.getPackageManager();
				try {
					holder.iconView.setImageDrawable(pm
							.getApplicationIcon(app.packageName));
				} catch (NameNotFoundException e) {
				}
				holder.titleView.setText(app.mLabel);
				holder.pkgName.setText(app.packageName);
				holder.ver.setText(app.mVer);
				holder.shareUid.setText(app.mShareUid);

			}
			return convertView;
		}
	}

	private static class ViewHolder {
		ImageView iconView;
		TextView titleView;
		TextView pkgName;
		TextView ver;
		TextView shareUid;
	}

	static class MyHandler extends Handler {
		private AppListAdapter mListAdapter;
		private Context mCtx;

		private MyHandler(Context ctx) {
			mCtx = ctx;
		}

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case MSG_REFRESH_LIST:
				if (mCtx != null) {
					if (mCtx != null) {
						mListAdapter = new AppListAdapter(mCtx, mAppList);
						mListV.setAdapter(mListAdapter);
						mWaitLayout.setVisibility(View.GONE);
						mListV.setVisibility(View.VISIBLE);
					}
				}
				break;

			case MSG_LOADING_LIST:
				mWaitLayout.setVisibility(View.VISIBLE);
				mListV.setVisibility(View.GONE);
				break;
			}
		}

	};

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		if (parent == mListV) {
			AppModel app = mAppList.get(position);
			if (app != null) {
				Intent intent = new Intent();
				switch (mCatalogType) {
				case AppModel.APP_TYPE_RUNNING:
					intent.putExtra(AppDetailActivity.APP_MODEL, app);
					intent.setClass(mCtx, AppDetailActivity4Running.class);
					break;
				default:
					intent.putExtra(AppDetailActivity.APP_MODEL, app);
					intent.setClass(mCtx, AppDetailActivity.class);
					break;
				}
				intent.putExtra(AppDetailActivity.PKG_NAME, app.packageName);
				intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				this.startActivity(intent);
			}

		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		}

	}

}